package com.intretech.umsin.system.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
import com.intretech.umsin.system.param.SysOperLogParam;
import com.intretech.umsin.system.mapper.SysOperLogMapper;
import com.intretech.umsin.system.entity.SysOperLogEntity;
import com.intretech.umsin.system.service.SysOperLogService;
import com.intretech.umsin.system.vo.SysOperLogVo;
import com.intretech.umsin.system.converter.SysOperLogVoEntityConverter;



/**
* @version V1.0
* 操作日志记录:sys_oper_log 对应商业服务实现.
* @ClassName: SysOperLogServiceImpl
* @Description: 操作日志记录:sys_oper_log 对应商业服务实现.
* @author: 李启联
* @date:   2022-8-16 16:25:07
* @Copyright: Copyright (c) 2022
*/
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLogEntity> implements SysOperLogService {

    @Autowired
    private SysOperLogVoEntityConverter sysOperLogVoEntityConverter;


    /**
    * 根据ID查询单个操作日志记录对象
    * @param id 组键ID
    * @return 单个操作日志记录VO对象
    */
    public SysOperLogVo queryById(Serializable id){
        if (id == null) {return null;}
        return sysOperLogVoEntityConverter.reverseFromTarget(this.getById(id));
    }


    /**
    * 根据ID集合查询返回操作日志记录列表
    * @param idList 组键ID集合
    * @return 操作日志记录列表
    */
    public List<SysOperLogVo> queryByIds(Collection<? extends Serializable> idList){
        if(CollectionUtils.isEmpty(idList)){return null;}
        return sysOperLogVoEntityConverter.reverseFromTarget(this.listByIds(idList));
    }


    /**
    * 分页查询 操作日志记录 信息
    * @param page 分页对象
    * @param param 查询参数
    * @return IPage<SysOperLogVo> 分页对象
    */
    @Override
    public IPage<SysOperLogVo> queryForPage(IPage page,SysOperLogParam param){
      IPage<SysOperLogEntity> page0 = this.page(page,createQueryWrapper(param));
      IPage<SysOperLogVo> pageResult = page0.convert(e -> sysOperLogVoEntityConverter.reverseFromTarget(e));
      return pageResult;
    }


    /**
    * 根据条件查询返回 操作日志记录 对象列表
    * @param  param 查询参数
    * @return 操作日志记录 对象列表
    */
    @Override
    public List<SysOperLogVo> queryForList(SysOperLogParam param) {
        List<SysOperLogEntity> list = this.list(createQueryWrapper(param));
        return sysOperLogVoEntityConverter.reverseFromTarget(list);
    }

    /**
    * 构建查询条件
    * @param param 查询参数实体对象
    * @return 构建后的QueryWrapper查询条件
    */
    private QueryWrapper<SysOperLogEntity> createQueryWrapper(SysOperLogParam param){
        QueryWrapper<SysOperLogEntity> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(param)) { return queryWrapper; }
        Optional<SysOperLogParam> op = Optional.of(param);
        queryWrapper.lambda()
                .eq(op.map(SysOperLogParam::getId).isPresent(),SysOperLogEntity::getId,param.getId())
                .eq(op.map(SysOperLogParam::getTitle).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getTitle,param.getTitle())
                .eq(op.map(SysOperLogParam::getBusinessType).isPresent(),SysOperLogEntity::getBusinessType,param.getBusinessType())
                .eq(op.map(SysOperLogParam::getMethod).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getMethod,param.getMethod())
                .eq(op.map(SysOperLogParam::getRequestMethod).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getRequestMethod,param.getRequestMethod())
                .eq(op.map(SysOperLogParam::getOperatorType).isPresent(),SysOperLogEntity::getOperatorType,param.getOperatorType())
                .eq(op.map(SysOperLogParam::getOperName).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getOperName,param.getOperName())
                .eq(op.map(SysOperLogParam::getDeptName).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getDeptName,param.getDeptName())
                .eq(op.map(SysOperLogParam::getOperUrl).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getOperUrl,param.getOperUrl())
                .eq(op.map(SysOperLogParam::getOperIp).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getOperIp,param.getOperIp())
                .eq(op.map(SysOperLogParam::getOperLocation).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getOperLocation,param.getOperLocation())
                .eq(op.map(SysOperLogParam::getOperParam).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getOperParam,param.getOperParam())
                .eq(op.map(SysOperLogParam::getJsonResult).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getJsonResult,param.getJsonResult())
                .eq(op.map(SysOperLogParam::getStatus).isPresent(),SysOperLogEntity::getStatus,param.getStatus())
                .eq(op.map(SysOperLogParam::getErrorMsg).filter(StringUtils::isNotBlank).isPresent(),SysOperLogEntity::getErrorMsg,param.getErrorMsg())
                .eq(op.map(SysOperLogParam::getOperTime).isPresent(),SysOperLogEntity::getOperTime,param.getOperTime())
        ;
        return queryWrapper;
    }

    /**
    * 添加操作日志记录到数据库
    * @param vo  操作日志记录 VO对象
    * @return 添加后的操作日志记录 VO对象
    */
    @Override
    public SysOperLogVo add(SysOperLogVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        SysOperLogEntity entity = sysOperLogVoEntityConverter.convertToTarget(vo);
        if(this.save(entity)){
            return sysOperLogVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("添加 操作日志记录 单个对象失败!");
            //return null;
        }
    }

    /**
    * 添加操作日志记录集合列表到数据库
    * @param vos  操作日志记录 VO对象列表
    * @return 添加后的操作日志记录 VO对象集合列表
    */
    @Override
    public List<SysOperLogVo> adds(List<SysOperLogVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<SysOperLogEntity> entities = sysOperLogVoEntityConverter.convertToTarget(vos);
        if(this.saveBatch(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return sysOperLogVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
           throw new RuntimeException("添加 操作日志记录 集合列表失败!");
           //return null;
        }
    }

    /**
    * 更新操作日志记录到数据库
    * @param vo 要更新的操作日志记录 VO对象
    * @return 更新后的操作日志记录 VO对象
    */
    @Override
    public SysOperLogVo modify(SysOperLogVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        SysOperLogEntity entity = sysOperLogVoEntityConverter.convertToTarget(vo);
        if(this.updateById(entity)){
            return sysOperLogVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("更新操作日志记录单个对象失败!");
            //return null;
        }
    }

    /**
    * 更新操作日志记录集合列表到数据库
    * @param vos 要更新的操作日志记录 VO对象集合
    * @return 更新后的操作日志记录 VO对象集合
    */
    @Override
    public List<SysOperLogVo> modifys(List<SysOperLogVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<SysOperLogEntity> entities = sysOperLogVoEntityConverter.convertToTarget(vos);
        if(this.updateBatchById(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return sysOperLogVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
            throw new RuntimeException("更新操作日志记录集合列表失败!");
            //return null;
        }
    }	

}